package md.lanjing.com.module_mine.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.constant.Constant;
import com.lxh.baselibray.event.EventImpl;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

import md.lanjing.com.module_mine.R;
import md.lanjing.com.module_mine.R2;
import md.lanjing.com.module_mine.bean.DealCenterNacVacancyResponce;
import md.lanjing.com.module_mine.contact.MyWalletContact;
import retrofit2.Response;

/**
 * 收款方式
 */
@Route(path = ARouterConfig.Mine_receive_money_type_act)
public class ReceiveMoneyTypeActivity extends MvpActivity<MyWalletContact.MyWalletPresent> implements MyWalletContact.IMyWalletView {
    @BindView(R2.id.tv_add_look1)
    TextView tvAddLook1;
    @BindView(R2.id.tv_add_look2)
    TextView tvAddLook2;
    @BindView(R2.id.tv_add_look3)
    TextView tvAddLook3;

    String aliPngCode;
    String weixinPngCode;
    String bankNumber;
    @Override
    public void initData(Bundle savedInstanceState) {
        mPresent.getVacancy(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_receive_money_type;
    }


    @OnClick({R2.id.tv_add_look1, R2.id.tv_add_look2, R2.id.tv_add_look3})
    public void onViewClicked(View view) {
        Intent intent=null;
        int i = view.getId();
        if (i == R.id.tv_add_look1) {
            if (ObjectUtils.isEmpty(aliPngCode)) {
                intent = gotoAdd(0);
            } else {
                intent = gotoQuery(0);
            }

        } else if (i == R.id.tv_add_look2) {
            if (ObjectUtils.isEmpty(weixinPngCode)) {
                intent = gotoAdd(1);
            } else {
                intent = gotoQuery(1);
            }

        } else if (i == R.id.tv_add_look3) {
            if (ObjectUtils.isEmpty(bankNumber)) {
                intent = gotoAdd(2);
            } else {
                intent = gotoQuery(2);
            }

        }
        startActivity(intent);
    }

    @NonNull
    private Intent gotoQuery(int type) {
        Intent intent;
        intent=new Intent(this, LookReceiveMoneyTypeActivity.class);
        intent.putExtra(LookReceiveMoneyTypeActivity.TYPE,type);
        return intent;
    }

    @NonNull
    private Intent gotoAdd(int type) {
        Intent intent;
        intent=new Intent(this, UpReceiveMoneyTypeActivity.class);
        intent.putExtra(UpReceiveMoneyTypeActivity.TYPE,type);
        return intent;
    }

    @Override
    public void getVacancyData(Response<DealCenterNacVacancyResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            DealCenterNacVacancyResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;
             aliPngCode=data.getAlipayQrcode();
             weixinPngCode=data.getWeChatQrcode();
             bankNumber=data.getBankCard();
            if (!ObjectUtils.isEmpty(aliPngCode)){
                tvAddLook1.setText("查看");
            }
            if (!ObjectUtils.isEmpty(weixinPngCode)){
                tvAddLook2.setText("查看");
            }
            if (!ObjectUtils.isEmpty(bankNumber)){
                tvAddLook3.setText("查看");
            }

            SPUtils.putString(Constant.PURSE_ADDRESS,data.getReceiveAddress(),context);
            SPUtils.putString(Constant.WEIXIN_PNG,data.getWeChatQrcode(),context);
            SPUtils.putString(Constant.ALI_PNG,data.getAlipayQrcode(),context);
            SPUtils.putString(Constant.BANK_NAME,data.getBankName(),context);
            SPUtils.putString(Constant.BANK_NUMBER,data.getBankCard(),context);
            SPUtils.putString(Constant.BANK_OWNER,data.getOwnerName(),context);
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }



    @Override
    protected MyWalletContact.MyWalletPresent createPresent() {
        return new MyWalletContact.MyWalletPresent();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void upPayMent(EventImpl.PaymentEvent event){
        if (!ObjectUtils.isEmpty(event)){
            //上传收款方式成功
            mPresent.getVacancy(context);
        }
    }

    @Override
    public boolean useEventBus() {
        return true;
    }
}
